package com.terren.spider.run;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


import com.terren.spider.core.html.processor.BasicHtmlProcessor;
import com.terren.spider.core.html.processor.HtmlArticleSearchPageProcessor;
import com.terren.spider.entity.biz.ArticleSearch;
import com.terren.spider.entity.common.SpiderLog;
import com.terren.spider.entity.core.Entry;
import com.terren.spider.run.basic.BasicSpiderArticleSearchRunner;
import com.terren.spider.util.common.BeanUtil;
import com.terren.spider.util.common.LogUtil;

public class SpiderArticlePageSearchRunner extends BasicSpiderArticleSearchRunner{


	@Override
	public List<Entry> getEntrys() {
		List<Entry> entries = new ArrayList<>();
		try {
			tasks = spiderTaskService.getOneSpiderPageTask();//找到状态是2 的任务 取1个
			if (null!= tasks) {
				tasks.setStatus(10);//状态设成 10 表示 抓取内容任务正在运行
				spiderTaskService.updateStatus(tasks);//状态设成 10 表示正在运行
				entries = entryService.getEntryBySourceId(tasks.getSourceId());//找到同一来源下的所有entry
				for (Entry entry : entries) {
					String keyword = tasks.getKeyword();
					Long taskId = tasks.getTaskId();
					Integer totalRecordCount = tasks.getLimit();
					String beginTime = tasks.getBeginTime();
					String endTime = tasks.getEndTime();
					entry.setKeyword(keyword);
					entry.setTaskId(taskId);
					entry.setRecordLimit(totalRecordCount);
					entry.setBeginTime(beginTime);
					entry.setEndTime(endTime);
				}
			}
		} catch (Exception e) {
			LogUtil.logger.error("SpiderArticleUrlSearchRunner error", e);
			taskFaild();
		}
		
		return entries;
	}
	
	
	@Override
	public void postRun(Entry entry) {
		if (tasks!=null) {
			tasks.setStatus(1);//状态设成 1 表示完成抓取任务
			try {
				spiderTaskService.updateStatus(tasks);
			} catch (Exception e) {
				log.error("runTask error",e);
			}
		}
	}
	
	
	
	/**
	 * 搜索引擎 抓取链接任务需要重写run方法
	 */
	public void run() throws Exception {
		long startTime, endTime;
		startTime = System.currentTimeMillis();
		log.info("【爬虫开始】请耐心等待...");
		List<Entry> list = getEntrys();
		for (Entry entry : list) {
			//记录爬虫日志
			SpiderLog slog = new SpiderLog();
			String message = "抓取页面成功"+"[taskId:"+entry.getTaskId()+"]"+"[搜索引擎:"+entry.getEntryDesc()+"]"+"[关键词:"+entry.getKeyword()+"]"
					+(tasks.getSite()==null?"":"[site:"+tasks.getSite()+"]");
			Integer size = null;
			Integer status = 1;
			try{
				//记录爬虫日志
				slog = saveLog(entry);
				postInitEntry(entry);
				ArticleSearch param = new ArticleSearch();
				param.setTaskId(entry.getTaskId());//任务ID
				param.setSourceId(entry.getSourceId());//来源ID
				/*****
				 * `t_article_search_title` 查找所有链接 、标题等
				 */
				List<ArticleSearch> articleList = articleSearchService.findArticleSearchTitle(param);//
				Set<String> urlset = new HashSet<>();
				for (ArticleSearch articleSearch : articleList) {
					urlset.add(articleSearch.getUrl());
				}
			//	String[] urls = urlset.toArray(new String[urlset.size()]);
				String[] urls = BeanUtil.set2Array(urlset);
				size = urls.length;
				if (urls.length>0) {
					handler = spiderHandler(entry);
					BasicHtmlProcessor pageProcessor = handler.getHtmlFetchUtil().getPageProcessor();
					if (pageProcessor instanceof HtmlArticleSearchPageProcessor) {
						((HtmlArticleSearchPageProcessor) pageProcessor).setArticleList(articleList);
					}
					log.info("抓取关键词内容任务开始.."+"[taskId:"+entry.getTaskId()+"]"+"[搜索引擎:"+entry.getEntryDesc()+"]"+"[关键词:"+entry.getKeyword()+"]"
							+(tasks.getSite()==null?"":"[site:"+tasks.getSite()+"]"));
					handler.parsePage(urls);
				}
				else {
					log.info("[task:"+entry.getTaskId()+"]未找到相关链接");
				}
			}catch(Exception e){
				message = "[taskId:"+entry.getTaskId()+"]"+"[搜索引擎:"+entry.getEntryDesc()+"]"+"[关键词:"+entry.getKeyword()+"]"
						+(tasks.getSite()==null?"":"[site:"+tasks.getSite()+"]")
						+"抓取页面失败:"+e.getMessage();
				status = 0;//失败，出现异常
				throw e;
			}finally{
				postRun(entry);
				//更新爬虫日志
				updateLog(slog,entry,message,size,status);
			}
		
			
			
			postRun(entry);
		}
		endTime = System.currentTimeMillis();
		log.info("【爬虫结束】,共耗时约" + ((endTime - startTime) / 1000 / 60) + "分。");
	}

}
